TWB.New_Module({
	///////////////////////////
	name : "Tools",          //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////

	settings : {
		key_back : {
			type : "text",
			maxlength : 1
		},
		key_next : {
			type : "text",
			maxlength : 1
		},
		key_overviews : {
			type : "text",
			maxlength : 1
		},
		key_overview : {
			type : "text",
			maxlength : 1
		},
		key_map : {
			type : "text",
			maxlength : 1
		},
		key_main : {
			type : "text",
			maxlength : 1
		},
		key_barracks : {
			type : "text",
			maxlength : 1
		},
		key_stable : {
			type : "text",
			maxlength : 1
		},
		key_garage : {
			type : "text",
			maxlength : 1
		},
		key_smith : {
			type : "text",
			maxlength : 1
		},
		key_snob : {
			type : "text",
			maxlength : 1
		},
		key_place : {
			type : "text",
			maxlength : 1
		},
		key_statue : {
			type : "text",
			maxlength : 1
		},
		key_market : {
			type : "text",
			maxlength : 1
		},
		key_up : {
			type : "text",
			maxlength : 1
		},
		key_down : {
			type : "text",
			maxlength : 1
		},
		key_left : {
			type : "text",
			maxlength : 1
		},
		key_right : {
			type : "text",
			maxlength : 1
		},
		key_recruit1 : {
			type : "text",
			maxlength : 1
		},
		key_recruit2 : {
			type : "text",
			maxlength : 1
		},
		key_recruit3 : {
			type : "text",
			maxlength : 1
		},
		key_recruit4 : {
			type : "text",
			maxlength : 1
		}
	},	
	_SETTINGS : {},
	_SHORTCUTS : {},
	_JSHORTCUTS : {},
	_KSHORTCUTS : {},
	keyboard_shortcuts : true,
	Toogle_KeyShortcuts : function(){
		var el=TWB.gei("TWB-KeyShortcuts");
		TWB.Modules.Tools.keyboard_shortcuts=!TWB.Modules.Tools.keyboard_shortcuts;
		if(TWB.Modules.Tools.keyboard_shortcuts){
			// Turn on
			el.style.color="blue";
			window.addEventListener("keypress", TWB.Modules.Tools.KeyEvent, true);
			return true;
		}
		el.style.color="black";
		window.removeEventListener("keypress", TWB.Modules.Tools.KeyEvent, true);
		return false;
	},
	KeyEvent : function(key){
		var WOW=String.fromCharCode(key.which);
		if(TWB.logged(window._content) && TWB.Modules.Tools.keyboard_shortcuts && TWB.Modules.Icons.Screen()!="forum"){
			var gee=TWB._window().document.activeElement.nodeName;
			if(TWB.Modules.Tools._SHORTCUTS[WOW] && gee!="INPUT" && gee!="TEXTAREA"){
				eval(TWB.Modules.Tools._SHORTCUTS[WOW]);
			}
		}
	},
	Reload_KeyboardShortcuts : function(){
		TWB.Modules.Tools._SHORTCUTS={};
		// Reload all kscripts:
		var sh;
		for(sh in TWB.Modules.Tools._KSHORTCUTS){
			if(typeof TWB.Modules.Tools._SHORTCUTS[sh]=="undefined"){
				TWB.Modules.Tools._SHORTCUTS[sh]="";
			}
			TWB.Modules.Tools._SHORTCUTS[sh]+=TWB.Modules.Tools._KSHORTCUTS[sh];
		}
		// Reload all jscripts:
		for(sh in TWB.Modules.Tools._JSHORTCUTS){
			if(typeof TWB.Modules.Tools._SHORTCUTS[sh]=="undefined"){
				TWB.Modules.Tools._SHORTCUTS[sh]="";
			}
			TWB.Modules.Tools._SHORTCUTS[sh]+=TWB.Modules.Tools._JSHORTCUTS[sh];
		}
	},
	Add_KeyboardShortcut : function(key, action, jscript){
		if(typeof key=="object"){
			var _key;
			for(key_ in key){
				TWB.Modules.Tools.Add_KeyboardShortcut(key_,key[key_],jscript);
			}
			return;
		}
		if(typeof jscript=="undefined"){
			key=TWB.get_var(key);
			if(typeof TWB.Modules.Tools._KSHORTCUTS[key]=="undefined"){
				TWB.Modules.Tools._KSHORTCUTS[key]="";
			}
			TWB.Modules.Tools._KSHORTCUTS[key]+=action;
		}
		else{
			if(typeof TWB.Modules.Tools._JSHORTCUTS[key]=="undefined"){
				TWB.Modules.Tools._JSHORTCUTS[key]="";
			}
			TWB.Modules.Tools._JSHORTCUTS[key]+=action;
		}
	},
	Launch : function(){
		params={
			inn : TWB.settings,
			out : null
		};
		T=window.openDialog("chrome://twbooster/content/settings.xul", "","modal,chrome,dialog,resizable=yes,alwaysRaised", TWB.lang("twb_settings"), params, window);
		// Reload Shortcuts
		TWB.Modules.Tools.Reload_KeyboardShortcuts();
		// Reload JScripts
		if(params.out!=null){
			for(i=0; i<params.out.length; i++){
				TWB.set_var(params.out[i][0],params.out[i][1]);
			}
			if(TWB.gei("TWB-ConfSkip")){
				TWB.gei("TWB-ConfSkip").setAttribute("checked",TWB.get_var("auto_skip"));
			}
			if(TWB.gei("TWB-Sync")){
				TWB.gei("TWB-Sync").setAttribute("checked",TWB.get_var("syncon"));
			}
			TWB.gei("TWB-Notepad-El").setAttribute("rows",TWB.get_var("note_rows"));
			TWB.Modules.Jscripts.Load_Scripts();
			if(TWB.get_var("autodetect_server")){
				TWB.Processor.start("ad_servers","Modules.MainMenu.Detect","*",false);
			}
			else{
				TWB.Processor.stop("ad_servers");
			}
		}
	},
	Processes : function(){
		return window.openDialog("chrome://twbooster/content/running_processes.xul", "","chrome,dialog,resizable=yes,alwaysRaised", TWB.lang("processes"), {}, window);
	},
	Cache : function(dev){
		if((typeof dev=="undefined" && confirm(TWB.lang("uncache"))) || typeof dev!="undefined"){
			TWB.ResetPrefs();
			var boot=TWB.Cc["@mozilla.org/toolkit/app-startup;1"].getService(TWB.Ci.nsIAppStartup);
			boot.quit(TWB.Ci.nsIAppStartup.eForceQuit|TWB.Ci.nsIAppStartup.eRestart);
		}
	},
	// Lock Window
	ToggleLock : function(){
		window.setTimeout(function(){
			if(TWB.Modules.Tools.Locked){	
				// If it was checked meaning we are on the tab that we are locked on to, unlock it
				TWB.Modules.Tools.Locked=false;
				delete TWB.Modules.Tools.Lock;
				TWB.gei("TWB-Lock").setAttribute("checked",false);
			}
			else{
				// Otherwise lets lock onto this one
				TWB.Modules.Tools.Locked=true;
				TWB.Modules.Tools.Lock=gBrowser.selectedBrowser;
				TWB.gei("TWB-Lock").setAttribute("checked",true);
			}
		},250);
	},
	LockMonitor : function(){
		var wind = gBrowser.selectedBrowser;
		if(wind==TWB.Modules.Tools.Lock){
			TWB.Modules.Tools.Locked=true;
			TWB.gei("TWB-Lock").setAttribute("checked",true);
		}
		else{
			TWB.Modules.Tools.Locked=false;
			TWB.gei("TWB-Lock").setAttribute("checked",false);
		}
	},
	priority : 2,
	init : function(){
		TWB.Modules.Tools.Add_KeyboardShortcut({
			key_back		:	"TWB.Modules.BackNext.Back();",
			key_next		:	"TWB.Modules.BackNext.Next();",
			key_overviews		:	"TWB.Modules.Icons.GoTo('overview_villages');",
			key_overview		:	"TWB.Modules.Icons.GoTo('overview');",
			key_map			:	"TWB.Modules.Icons.GoTo('map');",
			key_main		:	"TWB.Modules.Icons.GoTo('main');",
			key_barracks		:	"TWB.Modules.Icons.GoTo('barracks');",
			key_stable		:	"TWB.Modules.Icons.GoTo('stable');",
			key_garage		:	"TWB.Modules.Icons.GoTo('garage');",
			key_smith		:	"TWB.Modules.Icons.GoTo('smith');",
			key_snob		:	"TWB.Modules.Icons.GoTo('snob');",
			key_place		:	"TWB.Modules.Icons.GoTo('place');",
			key_statue		:	"TWB.Modules.Icons.GoTo('statue');",
			key_market		:	"TWB.Modules.Icons.GoTo('market');",
			key_up			:	"TWB.Modules.TWMMap.North();",
			key_down		:	"TWB.Modules.TWMMap.South();",
			key_left		:	"TWB.Modules.TWMMap.West();",
			key_right		:	"TWB.Modules.TWMMap.East();",
			key_recruit1		:	"TWB.Modules.Bots.Recruit1();",
			key_recruit2		:	"TWB.Modules.Bots.Recruit2();",
			key_recruit3		:	"TWB.Modules.Bots.Recruit3();",
			key_recruit4		:	"TWB.Modules.Bots.Recruit4();",
		});
		if(TWB.gei("TWB-Lock")!=null){
			TWB.gei("TWB-Lock").addEventListener("click",TWB.Modules.Tools.ToggleLock,false);
			// We need to monitor tabs for lock
			TWB.Modules.Tools.Locked=false;
			var container = gBrowser.tabContainer;
			container.addEventListener("TabSelect", TWB.Modules.Tools.LockMonitor, false);
		}
		TWB.Modules.Tools.Toogle_KeyShortcuts();
		TWB.Modules.Tools.Reload_KeyboardShortcuts();
	}
});
